<?php
namespace  Home\Controller;
use Think\Controller;
use Com\DingdingAuth;

class WebsiteController  extends  HomeController{
	
	public function index(){
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		// 搜索
		$keyword = I('keyword', '', 'string');

        $condition = array('like','%' . $keyword . '%');

        $map['name|domain'] = array($condition,$condition,'_multi' => true );

 

        // 获取所有用户
		$data_list = D('Website')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();   

        $page = new \Common\Util\Page(D('Website')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
       
        foreach ($data_list as $key => &$val) {
            $customerid=$val['customer'];
            $val['kh_id']=$customerid;
            $val['c_user']=get_user_info($val['c_user'],"username");
            $val['customer']=get_customer_info($val['customer'],"customer_name");
            $val['server']=get_fields("Server",$val['server'],"server_name");
            $val['master']=get_fields("Master",$val['master'],"name");          
        //    $val['domain']=get_fields("Domain",$val['domain'],"name");
            $val['database']=get_fields("Database",$val['database'],"name");
            $val['test']=count_availability($val['id']);
            
			//$val['customer']="<a href='".U('customer/xq',array('id'=>$customerid))."' style='text-decoration:none'>".$val['customer']."</a>";            
			// $val['server']="<a href='".U('Server/index')."' style='text-decoration:none'>". $val['server']."</a>";                        
			// $val['master']="<a href='".U('Master/index')."' style='text-decoration:none'>". $val['master']."</a>";                        
		//	 $val['domain']="<a href='".U('Domain/index')."' style='text-decoration:none'>". $val['domain']."</a>";                                   
			// $val['database']="<a href='".U('Database/index')."' style='text-decoration:none'>". $val['database']."</a>";                        
			 $val['e_time']?$val['e_time']=date('Y-m-d',$val['e_time']):'';
        }


        $this->assign('HTTP_HOST', C('DING_HTTP_HOST'));
        $this->assign('page', $page->show());
        $this->assign('data_list', $data_list);
        $this->assign('user_info', $user_info);
        $this->assign('meta_title', '网站列表');
        $this->display();
	}
	
	
	public function add(){
		
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		if(IS_POST){
			
				
				$ding_id=$user_info['userid'];
				$uid=M('Employee')->where(array('ding_id'=>$ding_id))->getField('uid');
				
			    $_POST['s_time']=strtotime($_POST['s_time']);
			    $_POST['e_time']=strtotime($_POST['e_time']);
				$_POST['c_user']=$uid;
				$_POST['c_time']=time();
				$web=D('Website');
				$data=$web->create();				
				if($data){
					$id=$web->add();
					if($id){
						$this->success('新增成功',U('index'));
					}else{
						$this->error('新增失败');
					}
				}else{
					$this->error($web->getError());
				}

			}else{
            
				$customer=M("Customer")->field("id,customer_name as title")->select();
				$server=M("Server")->field("id,server_name as title")->select();
				$master=M("Master")->field("id,name as title")->select();
				$database=M("Database")->field("id,name as title")->select();
				
				$this->assign('customer', $customer);
				$this->assign('server', $server);
				$this->assign('master', $master);
				$this->assign('database', $database);
				$this->assign('user_info', $user_info);
				$this->assign('meta_title', '新增网站');
				$this->display();

			}
	}
	
	public function  edit($id){
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		$info=M('Website')->find($id);

			if(IS_POST){
				$server=M('Website');
				$_POST['s_time']=strtotime($_POST['s_time']);
				$_POST['e_time']=strtotime($_POST['e_time']);

				$data=$server->create();

				if($data){
					$res=$server->save();
					if($res){
						$this->success('更新成功',U('index'));
					}else{
						$this->error('更新失败');
					}
				}else{
					$this->error($server->getError());
				}

			}else{
				
				$customer=M("Customer")->field("id,customer_name as title")->select();
				$server=M("Server")->field("id,server_name as title")->select();
				$master=M("Master")->field("id,name as title")->select();
				$database=M("Database")->field("id,name as title")->select();
				
				$this->assign('info', $info);
				$this->assign('customer', $customer);
				$this->assign('server', $server);
				$this->assign('master', $master);
				$this->assign('database', $database);
				$this->assign('user_info', $user_info);
				$this->assign('meta_title', '编辑网站');
				$this->display();
			
			}

	}
	
	/**
	 * 详情页
	 */
	public function websiteinfo(){
		
		$id=I('id');
		$val = M('Website')->where('id='.$id)->find();
		
		$customerid=$val['customer'];
		
		$val['c_user']=get_user_info($val['c_user'],"username");
		$val['customer']=get_customer_info($val['customer'],"customer_name");
		$val['server']=get_fields("Server",$val['server'],"server_name");
		$val['master']=get_fields("Master",$val['master'],"name");
		
		$val['database']=get_fields("Database",$val['database'],"name");
		$val['test']=count_availability($val['id']);
				
		$val['s_time']?$val['s_time']=date('Y-m-d',$val['s_time']):'';
		$val['e_time']?$val['e_time']=date('Y-m-d',$val['e_time']):'';
		$val['c_time']?$val['c_time']=date('Y-m-d',$val['c_time']):'';
		
		$this->assign('val', $val);
		$this->assign('meta_title', '网站详情');
		$this->display();
		
	}
	/**
	 * 删除网站
	 */
// 	public function delWebsiteAjax(){
	
// 		$id = I('id');
// 		$re=M('Website')->where('id='.$id)->delete();
// 		if ($re) {
// 			$data['info']=1;
// 		}else{
// 			$data['info']=0;
// 		}
// 		$this->ajaxReturn($data);
// 	}
	


}


?>